package binding

import (
	"context"
	"encoding/json"
	"group-card/common/response"
	"group-card/rpc/card/cardclient"

	"group-card/api/internal/svc"
	"group-card/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type BindingBoundLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewBindingBoundLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindingBoundLogic {
	return &BindingBoundLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BindingBoundLogic) BindingBound(req *types.BoundBindingRequest) (resp *types.BindingResponse, err error) {
	_, err = l.svcCtx.Card.BindingBound(l.ctx, &cardclient.BindingBoundRequest{
		Id:     req.Id,
		UserId: req.UserId,
	})

	if err != nil {
		reqStr, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("卡片绑定失败,参数:%s,异常:%s", reqStr, err.Error())
		return &types.BindingResponse{Code: response.ErrCode, Message: response.StrErr(err.Error())}, nil
	}

	return &types.BindingResponse{
		Code:    response.SuccessCode,
		Message: "卡片绑定成功",
	}, nil

}
